//导入request.js请求工具
import request from '@/utils/request.js'

// 提案列表
export const getProposalList = (params) => {
  return request.get('admins/proposals/getAllProposals', { 
    params: {
      ...params,
      pageStatus: params.pageStatus // 确保 pageStatus 参数被传递
    }
  })
}

// 获取提案详情
export const getProposalDetails = (proposalId) => {
  return request.get(`/admins/proposals/getProposalById/${proposalId}`)
}

// 修改提案状态
export const updateProposalStatus = ({ pid, status, isOr, pageStatus, department, handing_result, filePaths, fileName, size }) => {
  return request.put(`admins/proposals/updateProposalStatus/${pid}`, null, {
    params: { 
      status,
      isOr,
      pageStatus,
      department,
      handing_result,
      filePaths,
      fileName,
      size
    }
  });
};

// 获取提案统计数据
export const getProposalStatistics = () => {
  return request.get('admins/proposals/statistics')
}

// 获取提案类型分布
export const getProposalTypeDistribution = () => {
  return request.get('admins/proposals/typeDistribution')
}

// 获取提案提交趋势
export const getProposalTrend = (startDate, endDate) => {
  const params = {}
  if (startDate) params.startDate = startDate
  if (endDate) params.endDate = endDate
  
  return request.get('admins/proposals/trend', { params })
}

// 导出提案变化趋势数据
export const exportProposalTrendData = (startDate, endDate) => {
  const params = new URLSearchParams()
  if (startDate) params.append('startDate', startDate)
  if (endDate) params.append('endDate', endDate)
  
  return request({
    url: `/admins/proposals/exportTrendData?${params.toString()}`,
    method: 'get',
    responseType: 'blob',
    timeout: 60000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  })
}

// 导出提案数据
export const exportProposals = (params) => {
  // 构建查询参数
  const queryParams = new URLSearchParams()
  
  // 添加所有参数
  if (params.pageStatus) queryParams.append('pageStatus', params.pageStatus)
  if (params.status && params.status !== 'all') queryParams.append('status', params.status)
  if (params.proposalType && params.proposalType !== 'all') queryParams.append('proposalType', params.proposalType)
  if (params.startDate) queryParams.append('startDate', params.startDate)
  if (params.endDate) queryParams.append('endDate', params.endDate)
  if (params.searchText) queryParams.append('searchText', params.searchText)
  if (params.department) queryParams.append('department', params.department)
  if (params.sort) queryParams.append('sort', params.sort)
  
  return request({
    url: `/admins/proposals/exportProposals?${queryParams.toString()}`,
    method: 'get',
    responseType: 'blob',
    timeout: 60000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    }
  })
}

// 获取提案部门列表
export function getProList() {
  return request({
    url: '/admins/getProList',
    method: 'get'
  })
}

// 上传附件
export const uploadAttachment = (formData) => {
  return request({
    url: 'admins/uploadCoverImg',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 添加退回提案的 API
export function sendBack(data) {
  return request({
    url: `/admins/proposals/senBack/${data.proposalId}`,
    method: 'put',
    params: {
      status: data.status,
      isOr: data.isOr,
      pageStatus: data.pageStatus
    }
  })
}

// 添加公示状态更新的 API
export const updatePublicStatus = ({ pid, status, isOr }) => {
  return request.put(`admins/proposals/publicity/${pid}`, null, {
    params: { 
      status,
      isOr
    }
  });
};

// 添加下载文件的API
export const downloadFile = (url) => {
  return request({
    url: `/admins/proposals/file/download`,
    method: 'get',
    timeout: 60000,
    params: { url },
    responseType: 'blob'  // 重要：指定响应类型为blob
  })
}

// 导出提案详情为Word文档
export const exportProposalDetailToWord = (proposalId) => {
  return request({
    url: `/admins/proposals/exportProposalDetail/${proposalId}`,
    method: 'get',
    responseType: 'blob',
    timeout: 60000,
    headers: {
      'Accept': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    }
  })
}

